package com.usian.ai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.ai.common.Result;
import com.usian.ai.common.enums.ResultCodeEnum;
import com.usian.ai.common.exception.exception.PasswordErrorException;
import com.usian.ai.common.exception.exception.UsernameNotExistException;
import com.usian.ai.mapper.AdminMapper;
import com.usian.ai.pojo.Admin;
import com.usian.ai.pojo.vo.LoginReqVo;
import com.usian.ai.service.AdminService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.type.Alias;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @TODO:
 * @Author:baimu
 * @Date:2024 12 03 11 32
 **/

@Service
@Slf4j
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {
    @Autowired
    AdminMapper adminMapper;

    /**
     * 全局
     * @param loginReqVo
     * @return
     */
    @Override
    public Result login(LoginReqVo loginReqVo) {

        QueryWrapper<Admin> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.lambda().eq(Admin::getUsername,loginReqVo.getUsername());
        //objectQueryWrapper.eq("username",loginReqVo.getUsername());

        Admin admin = adminMapper.selectOne(objectQueryWrapper);

        //用户名不存在
        if(null==admin){
            log.info("用户名不存在");
            throw new UsernameNotExistException(ResultCodeEnum.USERNAME_NOT_EXIST);

        }

        String password = admin.getPassword();
        //密码不正确
        if(!password.equals(loginReqVo.getPassword())){
            log.info("密码不正确");
            throw new PasswordErrorException(ResultCodeEnum.PASSWORD_ERROR);
        }

        log.info("登录成功");

        return Result.ok(ResultCodeEnum.SUCCESS);
    }
}
